package com.liuguang.gateway.config.advice;

import com.liuguang.common.enums.CommonResultEnum;
import com.liuguang.common.exceptions.GlobalException;
import com.liuguang.common.pojo.CommonResult;
import com.liuguang.common.utils.WebUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.context.annotation.Configuration;
import org.springframework.validation.BindException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @ClassName: ControllerAdvice
 * @Description：
 * @Author: wanghr
 * Date: 2:23 PM 2020/9/13
 * Version: 1.0
 */
@org.springframework.web.bind.annotation.ControllerAdvice
@ResponseBody
public class ControllerAdvice {
    private static final Logger logger = LoggerFactory.getLogger(ControllerAdvice.class);

    @ExceptionHandler(GlobalException.class)
    public CommonResult handleGlobalException(GlobalException e) {
        logger.info("出现全局异常");
        return CommonResult.fail(CommonResultEnum.FAIL.getMsg(),e.getMessage());
    }

    @ExceptionHandler(Throwable.class)
    public CommonResult handleException(Throwable e) {
        logger.info("出现异常");
//        e.printStackTrace();
        return CommonResult.fail(CommonResultEnum.FAIL.getMsg(),e.getMessage());
    }
    @ExceptionHandler(BindException.class)
    public CommonResult handleBindException(BindException e) {
        logger.info("参数绑定验证异常");
//        e.printStackTrace();
        return CommonResult.fail(CommonResultEnum.FAIL.getMsg(),e.getMessage());
    }
}
